<template>
	<div class="lietou-list content">
		<div class="lietou-right">
			<el-form ref="form" :inline="true" label-width="90px" size="small">
				<el-form-item label="手机号" label-width="80px">
					<el-input placeholder="请输入手机号" v-model="searchPhone" class="search-input" maxlength="11" clearable></el-input>
				</el-form-item>
				<el-form-item label="代理商名称">
					<el-input placeholder="请输入代理商名称" v-model="nickname" clearable class="search-input"></el-input>
				</el-form-item>
				<el-form-item style="margin:-2px 0 0 10px;">
					<el-button icon="el-icon-search" @click="searchData" class="buttonStyle">搜索</el-button>
					<el-button icon="el-icon-plus" type="success" @click="addHandle" class="buttonStyle2">添加</el-button>
				</el-form-item>
			</el-form>
			<el-table header-row-class-name="my-table-header" :data="tableData" class="answerTable" style="width: 100%;"
			 :header-cell-style="{ background: '#eee',color:'#555555'}" stripe :row-style="{height:'54px'}"
			 :highlight-current-row="true">
				<el-table-column fixed type="index" label="序列" width="60" column-key="id" align="center"></el-table-column>
				<el-table-column prop="nickname" label="代理商名称" align="center"></el-table-column>
				<el-table-column prop="bindCity" label="绑定城市" align="center"></el-table-column>
				<el-table-column prop="lietou_agent_persent" label="猎头分佣" align="center"></el-table-column>
				<el-table-column prop="qiye_agent_persent" label="岗位分佣" align="center"></el-table-column>
				<el-table-column prop="usertel" label="手机号码" align="center"></el-table-column>
				<el-table-column prop="lietou_count" label="名下猎头数量" align="center"></el-table-column>
				<el-table-column prop="company_count" label="名下企业数" align="center"></el-table-column>
				<el-table-column prop="create_time" label="注册日期" align="center" width="180"></el-table-column>

				<el-table-column align="center" label="操作" width="120">
					<template slot-scope="scope">
						<router-link :to="{path:'/cityPartnerDetail',query:{id:scope.row.id}}">
							<el-button type="primary" icon="el-icon-edit" size="small" class="buttonStyle">详情</el-button>
						</router-link>
					</template>
				</el-table-column>
			</el-table>
			<el-row type="flex" class="row-bg" justify="center" v-show="pageCount>1">
				<el-col :span="24" style="text-align:center;margin-top:50px">
					<el-pagination background layout="prev, pager, next" :page-size="pageSize" :page-count="pageCount" :current-page="pageNo"
					 @current-change="pageChange">
					</el-pagination>
				</el-col>
			</el-row>
		</div>
    <el-dialog title="添加代理商" :visible.sync="showAddWin" class="form">
      <el-form :model="form" :rules="rules" autocomplete="on" label-width="100px" ref="form">
        <el-form-item label="代理商名称" prop="username">
          <el-input v-model="form.username" autocomplete="on" type="text" placeholder="请输入代理商名称(企业名称或负责人名称)" clearable></el-input>
        </el-form-item>
        <el-form-item label="手机号码："  prop="phone">
          <el-input v-model="form.phone" autocomplete="on" maxlength="11"  clearable placeholder="请输入手机号"></el-input>
        </el-form-item> 
		  <el-form-item label="绑定城市："  prop="city">
          <el-cascader v-model="form.city" :options="cityOptions" @change="cityChange" :props="{value:'id', label: 'cityname', children:'citylist'}"></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddWin = false">取 消</el-button>
        <el-button type="primary" @click="onSubmitPwd">确 定</el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { validUsername } from '@/utils/validate'
	export default {
		name: 'CityPartner',
		computed: {
			...mapGetters([
				'token'
			])
		},
		data() {
			const validateUsername = (rule, value, callback) => {
				if (value.length < 1) {
					callback(new Error('代理商名称不能为空！'))
				} else {
					callback()
				}
			}
			const validatePassword = (rule, value, callback) => {
			  if (!validUsername(value)) {
				callback(new Error('请输入正确的手机号'))
			  } else {
				callback()
			  }
			}
			const validateCity = (rule, value, callback) => {
				if (value=='') {
					callback(new Error('请选择绑定城市！'))
				} else {
					callback()
				}
			}
			
			return {
				searchPhone: '',
				nickname:'',
				tableData: [], //表格数据
				pageCount: 0,
				pageNo: 1,
				pageSize: 10,
			   form:{
					username: '',
					phone: '',
					city:''
				},
				rules: {
					username: [{ required: true, trigger: 'blur', validator: validateUsername }],
					phone: [{ required: true, trigger: 'blur', validator: validatePassword }],
					city: [{ required: true, trigger: 'blur', validator: validateCity }]
				},
				cityOptions:[],//省市选择列表
				showAddWin: false,
				province:[]//所选省份id
			}
		},
		watch:{
		  //  '$route': function(newUrl,oldUrl){
				// if(oldUrl.name=='lietouList'){ //详情页面返回的
				// 	if(oldUrl.params.refresh){
				// 	  this.loadData();
				// 	}
				// }
		  //  }
		},
		async created() {
		   this.loadData();
			this.getProvinceList();//获取省份列表
		},
		methods:{
			// 搜索
			searchData(){
				this.pageNo=1;
				this.loadData();
			},
			//分页
			pageChange(val) {
			  this.pageNo = val;
			  this.loadData()
			},
			async loadData() {
				const loading = this.$loading({
					lock: true,
					text: 'Loading',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.5)'
				});
				try {
					let data = {
						"token": this.token,
						"mobile": this.searchPhone,
						"nickname": this.nickname,
						"p": this.pageNo, //分页页码
						"size": this.pageSize, //分页每页数量
						"is_agent_qingtui": 1
					}
					let res = await this.$http({
						url: '/man/Agent/getList',
						method: 'post',
						params: data
					});
					this.tableData = res.data;
					this.pageCount = res.max_page; //总页数
					this.pageNo = Number(res.p); //当前页 size当前页
					loading.close();
			  } catch (e) {
			    console.log(e)
			    loading.close();
			  }
			},

			// 添加
			addHandle(){
				this.showAddWin = true;
			},
			// 确定添加
			onSubmitPwd(){
			  this.$refs.form.validate(async valid => {
				 if(valid){
					 let data = {
					 	"token": this.token,
						"id":'',
					 	"mobile": this.form.phone,
					 	"password": '123456', 
					 	"nickname": this.form.username,
						"cid": this.province[1]
					 }
					 let res = await this.$http({
					 	url: '/man/Agent/setInfo',
					 	method: 'post',
					 	params: data
					 });
					 this.$message({
					 	type: 'success',
					 	message: res.msg
					 });
					this.showAddWin = false;
					this.searchData();
				 }
			  })
			},
			
			// 获取省份列表
			async getProvinceList() {
				try {
					let res = await this.$http({
						url: '/Home/Region/getPC',
						method: 'post',
						params: {}
					});
					this.cityOptions = res.data;
			  } catch (e) {
			    console.log(e)
			  }
			},
			// 省市列表选择
			cityChange(val){
				this.province = val;
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.el-button.buttonStyle{
	  height: 32px;
	  font-size: 14px;
	  font-family: PingFangSC-Medium, PingFang SC;
	  font-weight: 500;
	  color: #FFFFFF;
	  background:#3674FF;
	  border-radius: 5px;
	  border:0;
	  margin-left:6px;
	  margin-top:2px;
	}
  .el-button.buttonStyle2{
    font-size:14px;
    border-radius:5px;
    margin-left:22px;
  }
	.answerTable{
		margin-top:15px;
	}
	.lietou-right{
		margin-top:30px;
	}
</style>
<style>
	.el-message{
		top:120px !important;
	}
</style>
